package com.wanda.controller;

import java.io.OutputStream;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wanda.model.ContractModel;
import com.wanda.model.CustomerModel;
import com.wanda.model.JsonGeneralModel;
import com.wanda.model.OrderModel;
import com.wanda.model.Wd_UserModel;
import com.wanda.service.ContractService;
import com.wanda.service.CustomerService;
import com.wanda.service.OrderService;
import com.wanda.util.BaseMultiController;
import com.wanda.util.ReportExcel;
import com.wanda.util.SendMassage;

@Controller
@RequestMapping("qt/contract") 
public class ContractController   extends BaseMultiController {

	@Autowired  
	private  HttpServletRequest request;  
	
	@Resource
	private ContractService contractService;
	 
	@Resource
	private OrderService orderService;
	//查询全部合同
	
	@Resource
	private CustomerService customerService;

	
	private List<ContractModel> contractList = new ArrayList<ContractModel>(); 

	private int checknum=0;
	
	public int getChecknum() {
		return checknum;
	}

	public void setChecknum(int checknum) {
		this.checknum = checknum;
	}
    private ContractModel cmd;
	

	public ContractModel getCmd() {
		return cmd;
	}

	public void setCmd(ContractModel cmd) {
		this.cmd = cmd;
	}

	@RequestMapping(value="/selectAll.do") 
	public @ResponseBody JsonGeneralModel selectAll(){
		System.out.println("查询所有合同");
		JsonGeneralModel<ContractModel> jsonModel = new JsonGeneralModel();
		List<ContractModel> Contracts =null;
		try {
			//String delete_state="0"; 
			Contracts = contractService.selectAll();
			jsonModel.setRows(Contracts);
		}catch (Exception e) {
			e.printStackTrace();
		}
			return jsonModel;
	    }
	
	//新增合同
	@RequestMapping(value="/addContract.do")
    public @ResponseBody Map<String,Object> add(
    		String order_id,String first_party,String second_party,
    		String room_area,String room_code,
    		String sign_time,String contract_content,String contract_total
    		){
		System.out.println("=============开始添加订单数据"); 
		List<OrderModel> orders = orderService.selectOrderAndRoomBySql("and order_id='"+order_id+"'");
		OrderModel order = orders.get(0);
		Calendar now = Calendar.getInstance();
	    String date =  " "+now.get(Calendar.HOUR_OF_DAY)+":"+now.get(Calendar.MINUTE)+":"+now.get(Calendar.SECOND);
		ContractModel contract = new ContractModel();
		contract.setContract_content(contract_content.trim());
		contract.setSign_time(sign_time+date);
		contract.setContract_total(contract_total);
		contract.setFirst_party(first_party);
		contract.setCustomer_id(order.getCustomer_id());
		contract.setCustomer_name(order.getCustomer_name());
		contract.setOrder_id(order_id);
		contract.setSecond_party(second_party);
		contract.setName(order.getName());
		contract.setUser_id(order.getUser_id());
		contract.setRoom_id(order.getRoom_id());
		contract.setBuild_up_area(room_area);
		contract.setUnit_price(order.getUnit_price());
		contract.setTotal(order.getTotal());
		contract.setPay_type_name(order.getOrder_type());
		contract.setContract_state(0);  //0是未签约
		contract.setBuild_name(order.getBuild_name());
		contract.setUnit_code(order.getUnit_code());
		contract.setRoom_code(room_code);
		contractList.add(contract);
		contractService.addContract(contract);
    	Map<String,Object> map = new HashMap<String,Object>();  
    	
    	System.out.println("添加成功"); 
    	map.put("msg","成功");
    	return map;
    }
	
	
	//删除订单  修改删除状态
	@RequestMapping(value="/deleteContract.do")
    public @ResponseBody Map<String,Object> delete(String contract_id){
		System.out.println("=============开始删除订单数据"); 
		ContractModel contract = new ContractModel();
		contract.setContract_id(contract_id);
		Timestamp timestamp = new Timestamp(System.currentTimeMillis());
		System.err.println(timestamp);
		String username = ((Wd_UserModel)request.getSession().getAttribute("bean")).getName();
		contract.setDelete_user(username);
		contract.setDelete_time(timestamp.toString());
		contractService.deleteContract(contract);
    	Map<String,Object> map = new HashMap<String,Object>();  
    	System.out.println("删除成功"); 
    	map.put("msg","成功");
    	return map;
    }
	
	@RequestMapping(value="/sendMassage.do")
    public @ResponseBody JsonGeneralModel<String>  sendMassage(String contract_id){
		JsonGeneralModel<String> jsonModel = new JsonGeneralModel<String> ();
		List<String> list = new ArrayList<String> ();
		SendMassage send = new SendMassage();
		if(!contract_id.isEmpty()){
			String sqlCustomer = "and customer_id = '"+contract_id+"'";
			List<CustomerModel> customerList = customerService.selectBySql(0, 10, sqlCustomer);
			if(!customerList.isEmpty()){
				System.out.println("给用户发短信"+customerList.get(0).getTel());//这个值没有被查到
				setChecknum(send.sendMassage(customerList.get(0).getTel()));
				list.add(customerList.get(0).getTel());
				jsonModel.setRows(list);
			}
		}
		return jsonModel;

    }
	
	@RequestMapping(value="/checkMassage.do")
    public @ResponseBody Map<String,Object> updateContractState(int cheknum,HttpServletResponse resp){
		Map<String,Object> map = new HashMap<String,Object>();  
    	SendMassage send = new SendMassage();
    	List<ContractModel> listcm = new ArrayList<ContractModel>();
		if(!(cheknum==getChecknum())){  //账户出错
	    	map.put("msg","签约成功");
	    	//orderPrintExcel(resp);
		}else{
	    	map.put("msg","验证错误");
	    	
		}
		return map;
    }
	
	//通过拼接sql实现多条件查询
	@RequestMapping(value="/selectBySql.do") 
	public @ResponseBody JsonGeneralModel selectBySql(String SearchSql){
		 System.err.println("SearchSql:"+SearchSql);
		 JsonGeneralModel<ContractModel> jsonModel = new JsonGeneralModel();
			List<ContractModel> contracts =null;
			try {
				//delete_state="0";
				contracts=contractService.selectBySql(SearchSql);
				jsonModel.setRows(contracts);
			}catch (Exception e) {
				e.printStackTrace();
			}
			return jsonModel;  
	}
	
	@RequestMapping(value="/exportExcel.do")
	public void orderPrintExcel(HttpServletResponse reps,HttpServletRequest re){
		ReportExcel report = new ReportExcel();
		try {
			if(!(contractList.isEmpty())){
				List<Hashtable<String, String>> list = report.orderPrint(contractList);
				OutputStream os;
				os = reps.getOutputStream();
				System.out.println("contractList"+contractList);
				reps.setContentType("application/octet-stream");
				reps.setHeader("Content-Disposition", "attachment;filename="
						+ java.net.URLEncoder.encode("签约合同.xlsx", "UTF-8"));
				report.getPrintFile(list, os);
				contractList.clear();;
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	
}
